import {defineStore} from 'pinia'
import {ElMessage} from 'element-plus'
import {
    addComment,
    cancelCollectArticleDetail,
    cancelConcernUser,
    cancelLoveArticleDetail,
    collectArticleDetail,
    concernUser,
    editNews,
    getChannelList,
    getCommentList,
    getDetailNews,
    getFirstCommentList,
    getHotNewsList,
    getHotTitle,
    getNewsList,
    getPersonNewsList,
    isConcernUser,
    loveArticleDetail,
    removeNewsPerson,
    saveNews
} from '@/service/index.js'
import hasDuplicate from '@/utils/filterComment.js'

const useArticleStore = defineStore('article', {
    state: () => ({
        channelList: '',
        hotTitleList: '',
        newsList: '',
        hotNewsList: '',
        searchParam: '',
        channelId: '-1',
        newsInfo: '',
        commentList: '',
        commentTwo: '',
        isLoading: false,
        currentPage: 2,
        loadNews: [],
        personNewsList: '',
        isConcern: "",
        index: -1

    }),
    actions: {
        //获取频道列表
        async getChannelListAction() {
            const res = await getChannelList()
            this.channelList = res.data
        },
        //获取热榜标题
        async getHotTitleAction() {
            const res = await getHotTitle()
            this.hotTitleList = res.data
        },
        //获取文章数据
        async getNewsListAction(page, size, title, channelId) {
            const res = await getNewsList(page, size, title, channelId)
            this.loadNews = res.data.records
        },

        //加载更多
        async loadIngMoreNews(title, channelId) {
            const res = await getNewsList(this.currentPage, 10, title, channelId)
            const arr = res.data.records

            this.loadNews.push(...arr)
            this.currentPage++
        },
        //获取前10条热点文章数据
        async getHotNewsList(size) {
            const res = await getHotNewsList(size)
            this.hotNewsList = res.data
        },
        //添加文章
        async saveNews(newsForm) {
            return await saveNews(newsForm)
        },
        async updateNewsAction(newsForm) {
            return await editNews(newsForm)
        },
        //获取文章详情
        async getDetailNews(newsId) {
            const res = await getDetailNews(newsId)
            this.newsInfo = res.data
        },
        //添加评论
        async addComment(comment) {
            return await addComment(comment)
        },
        //获取评论列表
        async getCommentListAction(newsId, commentId) {
            const res = await getCommentList(newsId, commentId)
            this.commentTwo = res.data
        },
        //获取评论一级列表
        async getFirstListAction(newsId) {
            const res = await getFirstCommentList(newsId)
            this.commentList = res.data
        },
        //获取个人信息
        async getPersonNewsListAction(page, size, searchParams) {
            const res = await getPersonNewsList(page, size, searchParams)
            this.personNewsList = res.data
        },
        async removePersonNewsAction(newsId) {
            const res = await removeNewsPerson(newsId)
            if (res.data.code === 1) {
                ElMessage.success('删除成功')
            }
        },
        // 点赞
        async loveArticleInfo(articleId) {
            const res = await loveArticleDetail(articleId)
        },
        // 取消点赞
        async closeLoveArticleInfo(newsId) {
            const res = await cancelLoveArticleDetail(newsId)
        },


        // 收藏
        async collectArticleInfo(articleId) {
            const res = await collectArticleDetail(articleId)
        },
        // 取消收藏
        async closeCollectArticleInfo(newsId) {
            const res = await cancelCollectArticleDetail(newsId)
        },
        // 关注
        async concern(concernUserId) {
            const res = await concernUser(concernUserId)

        },
        // 取消关注
        async cancelConcern(concernUserId) {
            const res = await cancelConcernUser(concernUserId)
        },
        // 是否关注
        async isConcernUser(concernUserId) {
            const res = await isConcernUser(concernUserId)
            this.isConcern = res.data
        },
    }
})

export default useArticleStore
